package good

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/good"
	goodReq "github.com/flipped-aurora/gin-vue-admin/server/model/good/request"
	"github.com/flipped-aurora/gin-vue-admin/server/model/goodclass"
	"gorm.io/gorm"
)

type LsGoodService struct{}

// CreateLsGood 创建lsGood表记录
// Author [yourname](https://github.com/yourname)
func (lsGoodService *LsGoodService) CreateLsGood(lsGood *good.LsGood) (err error) {
	err = global.GVA_DB.Create(lsGood).Error
	return err
}

// DeleteLsGood 删除lsGood表记录
// Author [yourname](https://github.com/yourname)
func (lsGoodService *LsGoodService) DeleteLsGood(ID string, userID uint) (err error) {
	err = global.GVA_DB.Transaction(func(tx *gorm.DB) error {
		if err := tx.Model(&good.LsGood{}).Where("id = ?", ID).Update("deleted_by", userID).Error; err != nil {
			return err
		}
		if err = tx.Delete(&good.LsGood{}, "id = ?", ID).Error; err != nil {
			return err
		}
		return nil
	})
	return err
}

// DeleteLsGoodByIds 批量删除lsGood表记录
// Author [yourname](https://github.com/yourname)
func (lsGoodService *LsGoodService) DeleteLsGoodByIds(IDs []string, deleted_by uint) (err error) {
	err = global.GVA_DB.Transaction(func(tx *gorm.DB) error {
		if err := tx.Model(&good.LsGood{}).Where("id in ?", IDs).Update("deleted_by", deleted_by).Error; err != nil {
			return err
		}
		if err := tx.Where("id in ?", IDs).Delete(&good.LsGood{}).Error; err != nil {
			return err
		}
		return nil
	})
	return err
}

// UpdateLsGood 更新lsGood表记录
// Author [yourname](https://github.com/yourname)
func (lsGoodService *LsGoodService) UpdateLsGood(lsGood good.LsGood) (err error) {
	err = global.GVA_DB.Model(&good.LsGood{}).Where("id = ?", lsGood.ID).Updates(&lsGood).Error
	return err
}

// GetLsGood 根据ID获取lsGood表记录
// Author [yourname](https://github.com/yourname)
func (lsGoodService *LsGoodService) GetLsGood(ID string) (lsGood good.LsGood, err error) {
	err = global.GVA_DB.Where("id = ?", ID).First(&lsGood).Error
	return
}

// GetLsGoodInfoList 分页获取lsGood表记录
// Author [yourname](https://github.com/yourname)
func (lsGoodService *LsGoodService) GetLsGoodInfoList(info goodReq.LsGoodSearch) (list []good.LsGood, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&good.LsGood{})
	var lsGoods []good.LsGood
	// 如果有条件搜索 下方会自动创建搜索语句
	if info.StartCreatedAt != nil && info.EndCreatedAt != nil {
		db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
	}
	if info.GoodsName != nil && *info.GoodsName != "" {
		db = db.Where("goods_name LIKE ?", "%"+*info.GoodsName+"%")
	}
	if info.GoodsType != nil && *info.GoodsType != "" {
		db = db.Where("goods_type = ?", *info.GoodsType)
	}
	err = db.Count(&total).Error
	if err != nil {
		return
	}

	if limit != 0 {
		db = db.Limit(limit).Offset(offset)
	}

	err = db.Find(&lsGoods).Error
	return lsGoods, total, err
}
func (lsGoodService *LsGoodService) GetTypesList() (list []goodclass.LsGoodClass, total int64, err error) {
	//limit := info.PageSize
	//offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&goodclass.LsGoodClass{})
	var lsGoods []goodclass.LsGoodClass
	// 如果有条件搜索 下方会自动创建搜索语句
	//if info.StartCreatedAt !=nil && info.EndCreatedAt !=nil {
	//	db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
	//}
	//if info.GoodsName != nil && *info.GoodsName != "" {
	//	db = db.Where("goods_name LIKE ?","%"+*info.GoodsName+"%")
	//}
	//if info.GoodsType != nil && *info.GoodsType != "" {
	//	db = db.Where("goods_type = ?",*info.GoodsType)
	//}
	err = db.Count(&total).Error
	if err != nil {
		return
	}

	//if limit != 0 {
	//	db = db.Limit(limit).Offset(offset)
	//}

	err = db.Find(&lsGoods).Error
	return lsGoods, total, err
}
func (lsGoodService *LsGoodService) GetLsGoodPublic() {
	// 此方法为获取数据源定义的数据
	// 请自行实现
}
